package j_util.data_structures.queue;

import j_util.data_structures.nodus.Nodus;

/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * This class is a special kind of queue, that distinguishes between
 * different priority elements and orders them by corresponding priority.
 * Maximum priority is assigned by zero int. Less priority the element has,
 * greater should be the number that identifies it;
 *
 * @author Jose Martin Rozanec;
 */
public class PriorityQueue {
    private QueueD[] queueArray;
    private int quantity;

    public PriorityQueue() {
        queueArray = new QueueD[1];
        queueArray[0] = new QueueD();
    }

    /**
     * This method allows to know if Queue object is empty.
     * Returns true if is empty, false if not;
     *
     * @return isEmpty - boolean;
     */
    public boolean isEmpty() {
        return quantity == 0;
    }

    /**
     * This method allows to enqueue an element;
     *
     * @param o        - Object;
     * @param priority - int;
     */
    public void enqueue(Object o, int priority) {
        if (queueArray.length < priority) {
            QueueD[] aux = new QueueD[priority];
            //System.arraycopy(queueArray, 0, aux, 0, queueArray.length);
            for (int i = queueArray.length; i < aux.length; i++) {
                aux[i] = new QueueD();
            }
            for (int i = 0; i < queueArray.length; i++) {
                aux[i] = queueArray[i];
            }
            queueArray = aux;
        }
        queueArray[priority - 1].enqueue(o);
        quantity++;
    }

    /**
     * This method allows to dequeue an element;
     */
    public void dequeue() {
        for (int i = 0; i < queueArray.length; i++) {
            if (!queueArray[i].isEmpty()) {
                queueArray[i].dequeue();
                break;
            }
        }
        quantity--;
    }

    /**
     * This method allows to get first Object in PriorityQueue;
     *
     * @return firstObject - Object;
     */
    public Object getFront() {
        for (int i = 0; i < queueArray.length; i++) {
            if (!queueArray[i].isEmpty()) {
                Nodus front = (Nodus) queueArray[i].getFront();
                return front.getData();
            }
        }
        return null;
    }
}

